package com.cskaoyan.service;

import com.cskaoyan.mapper.FeedbackMapper;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.model.bean.Footprint;
import com.cskaoyan.model.bo.footbo.FeedBackBo;
import com.cskaoyan.model.bo.footbo.FootDeleteBo;
import com.cskaoyan.model.vo.foodvo.FootPrintListVo;
import com.cskaoyan.model.vo.foodvo.FootVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FootServiceImpl implements FootService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    FeedbackMapper feedbackMapper;


    @Override
    public FootPrintListVo footprintList(Integer page, Integer size) {

        String username = (String) SecurityUtils.getSubject().getPrincipal();
        int id = userMapper.selectIdByUsername(username);
        FootPrintListVo footPrintListVo = new FootPrintListVo();
        int total = footprintMapper.selectNumByUserId(id);
        int totalPage = 0;
        if (size != null){
            totalPage = total/size + 1;
        }
        footPrintListVo.setTotalPages(totalPage);
        page = (page - 1)*size;

        List<Integer> goodsIds = footprintMapper.selectGoodsIdById(id,page,size);
        List<FootVo> footVos = new ArrayList<>();
        for (Integer goodsId : goodsIds) {
            FootVo footVo = goodsMapper.selectGoodsById(goodsId);
            footVos.add(footVo);
        }
        footPrintListVo.setFootprintList(footVos);

        return footPrintListVo;
    }

    @Override
    public void footprintDelete(FootDeleteBo footDeleteBo) {
        Footprint footprint = new Footprint();
        footprint.setDeleted(true);
        footprintMapper.updateByPrimaryKeySelective(footprint);
    }

    @Override
    public void feedbackSubmit(FeedBackBo feedBackBo) {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        int id = userMapper.selectIdByUsername(username);
        boolean hasPicture = false;
        if (feedBackBo.getPicUrls().size() != 0){
            hasPicture = true;
        }
        feedbackMapper.insertFeedBack(id,username,feedBackBo.getMobile(),feedBackBo.getFeedType(),feedBackBo.getContent(),hasPicture,feedBackBo.getPicUrls().toString());
    }
}
